package com.soriya.nestlive.common;

import java.util.LinkedHashMap;
import java.util.Map;

public final class GlobalThreadLocal {

    private static final ThreadLocal<Map<String, Object>> MY_LOCAL = new ThreadLocal<>();

    /**
     * 写入本地线程
     */
    public static void put(String key, Object val) {
        Map<String, Object> map = MY_LOCAL.get();
        if (map == null) {
            map = new LinkedHashMap<>();
        }

        map.put(key, val);
        MY_LOCAL.set(map);
    }


    /**
     * 获取本地线程
     */
    public static Object get(String key) {
        Map<String, Object> map = MY_LOCAL.get();
        if (map == null) {
            return null;
        }
        return map.getOrDefault(key, "");
    }


    /**
     * 获取管理员ID
     */
    public static Long getAdminId() {
        Object adminId = GlobalThreadLocal.get("adminId");
        if (adminId == null || adminId.toString().equals("")) {
            return 0L;
        }
        return Long.parseLong(adminId.toString());
    }

    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        Object adminId = GlobalThreadLocal.get("userId");
        if (adminId == null || adminId.toString().equals("")) {
            return 0L;
        }
        return Long.parseLong(adminId.toString());
    }




}
